package com.etnet.streamserver.view.servlet;

import com.etnet.streamserver.view.client.ClientManager;
import com.etnet.streamserver.view.client.ConnManager;
import com.etnet.streamserver.view.client.NssSenderManager;
import com.etnet.streamserver.view.client.ResultData;
import com.etnet.streamserver.view.client.request.NssRequest;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;

public class SearchSortServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    Logger log = Logger.getLogger(SearchSortServlet.class);

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String fieldid = request.getParameter("fieldid");
        String code = request.getParameter("code");
        String reqType = request.getParameter("reqType");
        String sortType = request.getParameter("sortType");
        String from = request.getParameter("from");
        String to = request.getParameter("to");

        String msgStr = NssSenderManager.sendMsgStr(reqType, "1", new String[] { fieldid, code, sortType, from, to, "",
                "", "" });
        
//        ConnManager connManager = ConnManager.getInstance();
//        connManager.sendMessage(msgStr);
//        ResultData rsData = connManager.getMessage(reqType);
        NssRequest nssrequest = new NssRequest();
        nssrequest.setReqID(reqType);
        nssrequest.setReqActionType("1");
        nssrequest.setData(new String[] { fieldid, code, sortType, from, to, "", "", "" });
        ClientManager clientManager = ClientManager.getInstance();
        clientManager.putMessage(nssrequest);
        try {
            Thread.sleep(200);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
        ResultData rsData = clientManager.getMessageResult(reqType);
        String result = "0";
        JSONObject obj = new JSONObject();
        String header = "";
        String content = "";
        if (rsData != null) {
            header = rsData.getHeader();
            content = rsData.getContent();
            result = "0";
        } else {
            result = "-1";
        }
        try {
            obj.put("resultCode", result);
            obj.put("reqStr", msgStr);
            obj.put("header", header);
            obj.put("content", content);
        } catch (JSONException e) {
            this.log.info("create json error!", e);
        }
        out.write(obj.toString());
        out.flush();
        out.close();
    }
}